package shop;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Create By IntelliJ IDEA
 *
 * @Author: jsonor
 * @Create-Time: 2017/10/10 11:19
 * @Description: 购物车显示
 */
@WebServlet(name = "shoppingCar", urlPatterns = {"/shoppingCar"})
public class ShoppingCarServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException {

    HttpSession session = request.getSession();
    Object things = session.getAttribute("car");
    if (things == null) {
      response.getWriter().println("购物车为空，暂时未添加任何物品");
    }

    Map<String, String> car = (Map<String, String>)things;

    response.getWriter().println("购物车中的数据是:");
    Set<Entry<String, String>> entries = car.entrySet();
    for (Entry<String, String> item : entries) {
      response.getWriter().println(item.getKey() + "=" + item.getKey() + "");
    }
  }
}
